(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.countryController', countryController);
	countryController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig',
			'authService', 'ajaxServiceFactory', '$filter'];
	function countryController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("countryLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getCcy : {
				url : 'do/cda/dataList/countryCurrency',
				method : 'post'
			}	
		});
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.gridOptions.multiSelect = dialogsModule.params.multiSelect == undefined ? false : dialogsModule.params.multiSelect;
			if(ctrl.dialogsModule.params.multiSelect == "true"){
				ctrl.gridOptions.multiSelect = true;
			}
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
			initCcy();
		};

		function initCcy(){
			var ccyOptions = {
					"data" : []
				};
			ajaxService.getCcy().then(function(response) {
				ctrl.country = response;
				var ccyDatas = [];
				if(ctrl.country && ctrl.country.data){
					angular.forEach(ctrl.country.data, function(data, index, array){
						if (ccyDatas.indexOf(data.value) === -1) {
							ccyDatas.push(data.value);
						}
					});
					
					if (ccyDatas.length > 0) {
						for (var i = 0; i < ccyDatas.length; i++) {
							ccyOptions.data.push({
								"value" : ccyDatas[i],
								"label" : ccyDatas[i]
							});
						}
					}
				}
				ctrl.currencyOptions = ccyOptions.data;
				}, angular.noop);
		}
		
		ctrl.reprocess = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.reprocess(ctrl.selectedRows[0]).then(function(response) {
				var routerInfo = response.data || {}, state = routerInfo.processState;
				delete routerInfo.processState;
				$scope.go(state, routerInfo);
			}, angular.noop);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			//ctrl.searchCriteria.countryCode = ctrl.dialogsModule.params.countryCode;
			angular.forEach(ctrl.dialogsModule.params, function(value, key) {
				ctrl.searchCriteria[key] = value;
			});
			
			ctrl.searchCriteria.filterCountryCodes = [];
			if(ctrl.dialogsModule.params.filterCountryCodes && ctrl.dialogsModule.params.filterCountryCodes != ""){
				if(angular.isArray(ctrl.dialogsModule.params.filterCountryCodes)){
					angular.forEach( ctrl.dialogsModule.params.filterCountryCodes, function ( filterCode, key){
						if ( filterCode.countryCode != null) {
							filterCode.countryCode = $filter('encode')(filterCode.countryCode);
							ctrl.searchCriteria.filterCountryCodes.push(filterCode.countryCode);
						}
					});
				}else if (ctrl.dialogsModule.params.filterCountryCodes.countryCode != null){
					ctrl.dialogsModule.params.filterCountryCodes.countryCode = $filter('encode')(ctrl.dialogsModule.params.filterCountryCodes.countryCode);
					ctrl.searchCriteria.filterCountryCodes.push(ctrl.dialogsModule.params.filterCountryCodes.countryCode);
				}
				
			} else if(ctrl.dialogsModule.params.filterCountryCodesForBu) {
				var resultArr = new Array();
				angular.forEach(ctrl.dialogsModule.params.filterCountryCodesForBu, function(obj, key) {
					var resultPropVal = $filter('encode')(obj['buCtrlData']);
					if(resultPropVal) resultArr.push(resultPropVal);
				});
				ctrl.searchCriteria.filterCountryCodes = resultArr;
			} else {
				ctrl.searchCriteria.filterCountryCodes = [];
			}
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);